using Microsoft.Practices.Unity;
using Unity.Extensions;
using Xunit;

namespace Unity.Extensions
{
    public class InjectionAndInterceptionInterceptionExtensionTest
    {
        [Fact]
        public void CanInjectAnInterceptedClass()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<InterceptionExtension>();
            container.Configure<IInterceptionConfiguration>().InterceptVirtual<SpyVirtual>(typeof(SpyVirtual).GetMethod("InterceptedMethod"),
                                                                                           new RecordingHandler());
            container.RegisterType<IDependencyClass, DependencyClass>();

            IDependencyClass obj = container.Resolve<IDependencyClass>();
            obj.CheckItOut();

            Assert.Equal(3, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("In Method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
        }

        public class DependencyClass : IDependencyClass
        {
            readonly SpyVirtual virtualSpy;

            public DependencyClass(SpyVirtual virtualSpy)
            {
                this.virtualSpy = virtualSpy;
            }

            public void CheckItOut()
            {
                virtualSpy.InterceptedMethod();
            }
        }

        public interface IDependencyClass
        {
            void CheckItOut();
        }

        public class SpyVirtual
        {
            public virtual void InterceptedMethod()
            {
                Recorder.Records.Add("In Method");
            }
        }
    }
}